package com.hx.vending.mqtt;

import com.hx.vending.bean.*;
import com.hx.vending.dao.*;
import com.hx.vending.service.IEquipmentService;
import com.hx.vending.util.DateUtil;
import com.hx.vending.util.PushUtils;
import com.hx.vending.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * Created by Administrator on 2018/12/18.
 */
public class RunnerShipment{

    private ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-mybatis.xml");
    private EquipmentMapper equipmentMapper = ioc.getBean(EquipmentMapper.class);
    private UserMapper userMapper = ioc.getBean(UserMapper.class);
    private FieldMapper fieldMapper = ioc.getBean(FieldMapper.class);
    private TrailsMapper trailsMapper = ioc.getBean(TrailsMapper.class);
    private GoodsMapper goodsMapper = ioc.getBean(GoodsMapper.class);
    private GoodsConsumeMapper goodsConsumeMapper = ioc.getBean(GoodsConsumeMapper.class);
    private OnlineOrderMapper onlineOrderMapper = ioc.getBean(OnlineOrderMapper.class);
    private EquipmentTypeCaseMapper equipmentTypeCaseMapper = ioc.getBean(EquipmentTypeCaseMapper.class);

    public void stock(String[] strs,Integer onlineOrderId) {
        //[设备id，货道位置，出货数量，机箱id]
        try{
            Equipment equipment = new Equipment();
            equipment.setEquipmentId(strs[0]);
            Equipment eqeq = equipmentMapper.selectByPrimary(equipment);
            Trails trails = new Trails();
            trails.setTrailsEquipmentId(eqeq.getEquipmentId());
            List<Trails> trailsList = trailsMapper.selectByExample(trails);
            for(int j = 1;j < strs.length;j+=3){
                for(int i = 0;i < trailsList.size();i++){
                    //System.out.println("货道位置================"+strs[j]);
                    //System.out.println("机箱id============="+strs[j+2]);
                    if(strs[j].equals(trailsList.get(i).getTrailsPosition()) && Integer.parseInt(strs[j+2]) == trailsList.get(i).getTrailsCaseId()){
                        if(ToolUtil.isNotEmpty(strs[j+1])){
                            //查询商品信息
                            Goods goods = new Goods();
                            goods.setGoodsId(trailsList.get(i).getTrailsGoodsId());
                            Goods selectGoods = this.goodsMapper.selectByPrimaryKeySelective(goods);
                            //添加商品消耗
                            GoodsConsume goodsConsume = new GoodsConsume();
                            //单价
                            goodsConsume.setGoodsConsumePrice(selectGoods.getGoodsPrice());
                            //设备id
                            goodsConsume.setGoodsConsumeEquipmentId(strs[0]);
                            OnlineOrder o = new OnlineOrder();
                            o.setOnlineOrderId(onlineOrderId);
                            OnlineOrder onlineOrder = onlineOrderMapper.selectByPrimaryKeySelective(o);
                            //时间
                            goodsConsume.setGoodsConsumeTime(onlineOrder.getOnlineOrderTime());
                            //类型 订单类型1线上 2线下
                            goodsConsume.setGoodsConsumeType("1");
                            //商品id
                            goodsConsume.setGoodsConsumeGoodsId(selectGoods.getGoodsId());
                            //订单号
                            goodsConsume.setGoodsOrderId(onlineOrderId);

                            //每个商品添加一条消耗数据
                            if (Integer.parseInt(strs[j+1]) > 1) {
                                for (int count = 0; count < Integer.parseInt(strs[j+1]); count++) {
                                    this.goodsConsumeMapper.insertSelective(goodsConsume);
                                }
                            }else {
                                this.goodsConsumeMapper.insertSelective(goodsConsume);
                            }
                            //修改货道库存
                            trailsList.get(i).setTrailsGoodsStock(trailsList.get(i).getTrailsGoodsStock() - Integer.parseInt(strs[j+1]));
                            this.trailsMapper.updateByPrimaryKeySelective(trailsList.get(i));

                            User user = new User();
                            user.setUserId(eqeq.getEquipmentUserId());
                            User u = userMapper.selectByPrimaryKey(user);

                           /* EquipmentTypeCase equipmentTypeCase = new EquipmentTypeCase();
                            equipmentTypeCase.setEquipmentCaseId(Integer.parseInt(strs[j+2]));
                            EquipmentTypeCase typeCase = equipmentTypeCaseMapper.selectByPrimaryKeySelective(equipmentTypeCase);*/
                            //货道编号
                            if(u.getUserStockNotice().equals("1") && trailsList.get(i).getTrailsGoodsStock() < u.getUserStoclNoticeNum()){
                                Field findField = new Field();
                                findField.setFieldId(eqeq.getEquipmentFieldId());

                                Field field = this.fieldMapper.selectByPrimary(findField);
                                String message = "库存不足警戒:\n设备ID:%s\n设备编号:%s\n机箱编号:%s\n场地名称:%s\n货道编号:%s\n商品名称:%s\n" +
                                        "商品备注:%s\n场地地区:%s\n详细地址:%s\n剩余库存:%s";

                                if (u.getUserAccount() != null && null != field) {
                                    PushUtils.getInstance().pushMessage(u.getUserAccount(), String.format(message, eqeq.getEquipmentId(),
                                            eqeq.getEquipmentNumber()+"号机",
                                            strs[j+2]+"号箱",
                                            field.getFieldName(),
                                            trailsList.get(i).getTrailsNumber(),
                                            selectGoods.getGoodsName(),
                                            selectGoods.getGoodsRemark(),
                                            field.getFieldRegion(),
                                            field.getFieldAddress(),
                                            trailsList.get(i).getTrailsGoodsStock()));
                                }
                            }
                        }
                        break;
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        }
    }
